package cn.itsy.web.controller;

import cn.itsy.pojo.Duct;
import cn.itsy.pojo.Ductype;
import cn.itsy.service.ductype.DucTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: MisterGay
 * @date: 2019-05-30 14:10
 */
@Controller
@RequestMapping(value = "ducType")
public class DucTypeController {
    @Resource(name = "ducTypeServiceimpl")
    private DucTypeService ducTypeService;

    @RequestMapping(value = "queryDucTypeAll", method = RequestMethod.GET)
    @ResponseBody
    public List<Ductype> queryDucTypeAll() {
        String sql = "select * from ductype ";
        List<Ductype> ductypes = this.ducTypeService.queryDucTypeAll(sql);
        return ductypes;
    }

    @RequestMapping(value = "insertDuctype", method = RequestMethod.GET)
    @ResponseBody
    void insertDuctype(Ductype ductype) {
        String sql = "insert into ductype values(?,?,?)";
        boolean b = this.ducTypeService.insertDucType(sql, ductype);
    }

    @RequestMapping(value = "deleteDucTypeById", method = RequestMethod.GET)
    @ResponseBody
    Integer deleteDucTypeById(Ductype ductype) {
        String sql = "select * from duct where duct.ductype_id=?";
        List<Duct> ducts = this.ducTypeService.queryDuctById(sql, ductype);
        if (ducts.isEmpty()) {
            String delSql = "delete from ductype where id=?";
            Integer integer = this.ducTypeService.deleteDuctypeById(delSql, ductype);
            if (integer != 0) {
                return 1;
            }
        }
        return 0;
    }

    @RequestMapping(value = "updateDuctById", method = RequestMethod.GET)
    @ResponseBody
    void updateDuctById(Ductype ductype) {
        String sql = "update ductype set ductype.tbian=?,ductype.tname=? where ductype.id=?";
        Integer integer = this.ducTypeService.updateDuctNameById(sql, ductype);
    }

}
